package com.control;

import com.dbutil.UtilJdbc;
import com.entity.Role;
import com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SearchServlet extends HttpServlet {



    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException  {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String uname = request.getParameter("uname");
        String upassword = request.getParameter("upassword");
        String type = request.getParameter("type");

        if(!"".equals(uname) && !"".equals(upassword) ) {//用户名、密码不能为空，密码与确认密码是相同
            UtilJdbc utilJdbc = new UtilJdbc();
            boolean jg = utilJdbc.regit(uname, upassword);
            Role role = utilJdbc.findRole(type);
            User user=utilJdbc.login(uname,upassword);
            Integer uid=user.getId();
            Integer rid=role.getId();
            boolean flag=utilJdbc.doUserRole(uid,rid);
            if (flag){}
            if(jg) {
                    request.getRequestDispatcher("/LoginPag/RegSuccess.jsp").forward(request, response);
            }else{
                System.out.println("注册失败！回首页");
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            }
        }
    }
}
